package com.clear.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.clear.subject.application.convert.SubjectInfoDTOConverter;
import com.clear.subject.application.convert.SubjectLikedDTOConverter;
import com.clear.subject.application.dto.SubjectInfoDTO;
import com.clear.subject.application.dto.SubjectLikedDTO;
import com.clear.subject.common.entity.PageResult;
import com.clear.subject.common.entity.Result;
import com.clear.subject.common.util.LoginUtil;
import com.clear.subject.domain.entity.SubjectInfoBO;
import com.clear.subject.domain.entity.SubjectLikedBO;
import com.clear.subject.domain.service.SubjectLikedDomainService;
import com.clear.subject.infra.basic.entity.SubjectLiked;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/subject/liked/")
public class SubjectLikedController {

    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;


    /**
     * 新增题目 或 取消点赞
     *
     * @param subjectLikedDTO
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "点赞状态不能为空");

            String loginId = LoginUtil.getLoginId();
            subjectLikedDTO.setLikeUserId(loginId);
            Preconditions.checkNotNull(StringUtils.isNotBlank(subjectLikedDTO.getLikeUserId()), "点赞人id不能为空");

            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);

            subjectLikedDomainService.add2(subjectLikedBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLikedController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增题目点赞失败");
        }
    }

    /**
     * 查询我的点赞列表
     *
     * @param subjectLikedDTO
     * @return
     */
    @PostMapping("/getSubjectLikedPage")
    public Result<PageResult<SubjectLikedDTO>> getSubjectLikedPage(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.getSubjectLikedPage.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }

            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);

            PageResult<SubjectLikedBO> boPageResult = subjectLikedDomainService.getSubjectLikedPage(subjectLikedBO);

//            PageResult<SubjectLikedDTO> dtoPageResult = SubjectLikedDTOConverter.INSTANCE.convertBOToDTO(boPageResult);

            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectLikedController.getSubjectLikedPage.error:{}", e.getMessage(), e);
            return Result.fail("查询我的点赞列表失败");
        }
    }

}
